package com.aischool.service;

import java.util.List;

import com.aischool.utils.RegexUtil;
import com.aischool.utils.StringUtil;
import com.jfinal.aop.Inject;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.ehcache.CacheKit;

public class SysPermissionService {

	private final String CK_CacheName = "CachePower";
	//private final String CK_Sys_Permission_UID_Pre = "CK_Sys_Permission_UID_";
	
	@Inject
	SysUserRoleService sysUserRoleService;
	@Inject
	SysUserAppService sysUserAppService;
	
	
	private String getCKAppRole(int app_id, int role_id) {
		return "CK_Power_APPID_" + app_id + "_ROLEID_" + role_id;
	}
	
	private String getCKAppUser(int app_id, int user_id) {
		return "CK_Power_APPID_" + app_id + "UserID_" + user_id;
	}
	
	/**
	 * 获取应用用户菜单
	 * @return
	 */
	public List<Record> getAppUserMenu(int app_id, int user_id){
		return Db.findByCache(CK_CacheName, getCKAppUser(app_id, user_id), 
				"SELECT DISTINCT b.* \r\n" + 
				"FROM sys_permission a \r\n" + 
				"INNER JOIN sys_menu b ON b.menu_id = a.menu_id\r\n" + 
				"INNER JOIN sys_user_role c ON c.role_id = a.role_id\r\n" + 
				"WHERE a.app_id = ? AND c.user_id = ? AND b.menu_state = 1 "
				+ "ORDER BY b.parent_id, b.menu_sort, b.menu_id",
				app_id, user_id);
		
	}
	
	/**
	 * 获取应用角色菜单
	 * @return
	 */
	public List<Record> getAppRoleMenu(int app_id, int role_id){
		return Db.findByCache(CK_CacheName, getCKAppRole(app_id, role_id), 
				"SELECT a.role_id, b.* FROM sys_permission a \r\n" + 
				"INNER JOIN sys_menu b ON b.menu_id = a.menu_id\r\n" + 
				"WHERE a.app_id = ? AND a.role_id = ? AND b.menu_state = 1",
				app_id, role_id);
	}
	
	public boolean updatePower(int app_id, int role_id, String ids) {
		//boolean bf = false;
		// 先
		int ir = Db.delete("DELETE FROM sys_permission WHERE app_id = ? AND role_id = ?", app_id, role_id);
		if (StrKit.notBlank(ids) && RegexUtil.isSqlIds(ids)) {
			ir = Db.update("INSERT INTO sys_permission(app_id, role_id, menu_id, api_id) " + 
					"SELECT ?, ?, CAST(tt.ids AS INTEGER), 0 " + 
					"FROM (SELECT regexp_split_to_table(COALESCE(?,''),E',') AS ids) AS tt",
					app_id, role_id, StringUtil.trim(ids, ','));
		}
		removeCache();
		return ir>0;
//		
//		if (StrKit.isBlank(ids)) {
//			// 清空
//			Db.delete("DELETE FROM sys_permission WHERE app_id = ? AND role_id = ?", app_id, role_id);
//		}else {
//			// 修改
//			ids = "," + ids;
//			List<Record> listp = getAppRoleMenu(app_id, role_id);
//			for(Record rd : listp) {
//				ids = ids.replace("," + rd.getStr("menu_id") + ",", ",");
//			}
//			ids = StringUtil.trim(ids, ',');
//			if (StrKit.notBlank(ids)) {
//				String[] arr1 = ids.split(",");
//				int ir = 0;
//				for(int i = 0; i< arr1.length; i++) {
//					ir += Db.update("INSERT INTO sys_permission VALUES(?, ?, ?, ?) ON CONFLICT ON CONSTRAINT pk_sys_permission DO NOTHING",
//							app_id, role_id, Integer.parseInt(arr1[i]), 0);
//				}
//				bf = ir > 0;
//			}else {
//				bf = true;
//			}
//		}
//		if (bf) {
//			//CacheKit.remove(CK_CacheName, getCKAppRole(app_id, role_id));
//			removeCache();
//		}
//		return bf;
	}
	
	public void removeCache() {
		CacheKit.removeAll(CK_CacheName);
		sysUserRoleService.removeCache();
		sysUserAppService.removeCache();
	}
}
